<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>libcoap: coap_pdu_setup(3)</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">libcoap<span id="projectnumber">&#160;4.2.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.8 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('man_coap_pdu_setup.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">coap_pdu_setup(3)</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>coap_pdu_setup</title><link rel="stylesheet" type="text/css" href="docbook-xsl.css" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /></head><body><div xml:lang="en" class="refentry" lang="en"><a id="id1337"></a><div class="titlepage"></div><div class="refnamediv"><h2>NAME</h2><p>coap_pdu_setup, coap_pdu_init, coap_add_token, coap_new_optlist, coap_insert_optlist, coap_delete_optlist, coap_add_optlist_pdu, coap_add_option, coap_add_data — Work with CoAP PDUs</p></div><div class="refsynopsisdiv"><a id="_synopsis"></a><h2>SYNOPSIS</h2><p><span class="strong"><strong>#include &lt;coap2/coap.h&gt;</strong></span></p><p><span class="strong"><strong>coap_pdu_t *coap_pdu_init(uint8_t <span class="emphasis"><em>type</em></span>, uint8_t <span class="emphasis"><em>code</em></span>,
uint16_t <span class="emphasis"><em>message_id</em></span>, size_t <span class="emphasis"><em>max_size</em></span>);</strong></span></p><p><span class="strong"><strong>int coap_add_token(coap_pdu_t *<span class="emphasis"><em>pdu</em></span>, size_t <span class="emphasis"><em>length</em></span>,
const uint8_t *<span class="emphasis"><em>data</em></span>);</strong></span></p><p><span class="strong"><strong>coap_optlist_t *coap_new_optlist(uint16_t <span class="emphasis"><em>number</em></span>, size_t <span class="emphasis"><em>length</em></span>,
const uint8_t *<span class="emphasis"><em>data</em></span>);</strong></span></p><p><span class="strong"><strong>int coap_insert_optlist(coap_optlist_t **<span class="emphasis"><em>optlist_chain</em></span>,
coap_optlist_t *<span class="emphasis"><em>optlist</em></span>);</strong></span></p><p><span class="strong"><strong>void coap_delete_optlist(coap_optlist_t *<span class="emphasis"><em>optlist_chain</em></span>);</strong></span></p><p><span class="strong"><strong>int coap_add_optlist_pdu(coap_pdu_t *<span class="emphasis"><em>pdu</em></span>, coap_optlist_t **<span class="emphasis"><em>optlist_chain</em></span>);</strong></span></p><p><span class="strong"><strong>size_t coap_add_option(coap_pdu_t *<span class="emphasis"><em>pdu</em></span>, uint16_t <span class="emphasis"><em>number</em></span>, size_t <span class="emphasis"><em>length</em></span>,
const uint8_t *<span class="emphasis"><em>data</em></span>);</strong></span></p><p><span class="strong"><strong>int coap_add_data(coap_pdu_t *<span class="emphasis"><em>pdu</em></span>, size_t <span class="emphasis"><em>length</em></span>, const uint8_t *<span class="emphasis"><em>data</em></span>);</strong></span></p><p><span class="strong"><strong>void coap_add_data_blocked_response(coap_resource_t *<span class="emphasis"><em>resource</em></span>,
coap_session_t *<span class="emphasis"><em>session</em></span>, coap_pdu_t *<span class="emphasis"><em>request</em></span>, coap_pdu_t *<span class="emphasis"><em>response</em></span>,
const coap_binary_t *<span class="emphasis"><em>token</em></span>, uint16_t <span class="emphasis"><em>media_type</em></span>, int <span class="emphasis"><em>maxage</em></span>,
size_t <span class="emphasis"><em>length</em></span>, const uint8_t</strong></span> <span class="emphasis"><em>data</em></span>);</p><p><span class="strong"><strong>unsigned int coap_encode_var_safe(uint8_t *<span class="emphasis"><em>buffer</em></span>, size_t <span class="emphasis"><em>size</em></span>,
unsigned int <span class="emphasis"><em>value</em></span>);</strong></span></p><p><span class="strong"><strong>int coap_split_path(const uint8_t *<span class="emphasis"><em>path</em></span>, size_t <span class="emphasis"><em>length</em></span>, uint8_t *<span class="emphasis"><em>buffer</em></span>,
size_t *<span class="emphasis"><em>buflen</em></span>);</strong></span></p><p><span class="strong"><strong>int coap_split_query(const uint8_t *<span class="emphasis"><em>query</em></span>, size_t <span class="emphasis"><em>length</em></span>, uint8_t *<span class="emphasis"><em>buffer</em></span>,
size_t *<span class="emphasis"><em>buflen</em></span>);</strong></span></p><p>Link with <span class="strong"><strong>-lcoap-2</strong></span>, <span class="strong"><strong>-lcoap-2-gnutls</strong></span>,
<span class="strong"><strong>-lcoap-2-openssl</strong></span> or
<span class="strong"><strong>-lcoap-2-tinydtls</strong></span> depending on your (D)TLS library
type.</p></div><div class="refsect1"><a id="_description"></a><h2>DESCRIPTION</h2><p>The CoAP PDU is of the form</p><p>--header--|--optional token--|--optional options--|--optional payload--</p><p>The PDU must be built in the correct order, from left to right.  In particular,
the options need to be added in the correct numerical option order as they are
stored in the PDU using relative numeric offsets from the previous option
number.</p><p>There are option list functions available where options can be added to a
chained list of options and then the chain list is sorted and then be added to
the PDU.</p><p>Typically for clients, when creating a request, the PDU needs to be created
before filling it with the appropriate information.</p><p>Typically with a server, the response PDU, with the optional token already added
in, will already be created before the response handler is called, and the
response PDU will need to be updated as appropriate starting with the optional
options.  Note that updating the response pdu’s code variable will cause the
response pdu to get transmitted.  If code does not get updated, then the
response pdu is freed off by the underlying library.</p><p>The <span class="strong"><strong>coap_pdu_init</strong></span>() function returns a newly created <span class="emphasis"><em>PDU</em></span> of type
<span class="emphasis"><em>coap_pdu_t</em></span>*.  <span class="emphasis"><em>type</em></span> is one of the following</p><div class="horizontal"><table class="horizontal" cellpadding="4px" style="border: none;"><colgroup><col /><col /></colgroup><tbody valign="top"><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_MESSAGE_CON</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> to be of type confirmable.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_MESSAGE_NON</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> to be of type non-confirmable.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_MESSAGE_ACK</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> to be of type acknowledge (for internal use).
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_MESSAGE_RST</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> to be of type reset.
</p>
</td></tr></tbody></table></div><p>The <span class="emphasis"><em>code</em></span> is one of the following</p><div class="horizontal"><table class="horizontal" cellpadding="4px" style="border: none;"><colgroup><col /><col /></colgroup><tbody valign="top"><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_REQUEST_GET</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> request to be of type GET.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_REQUEST_POST</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> request to be of type POST.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_REQUEST_PUT</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> request to be of type PUT.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_REQUEST_DELETE</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> request to be of type DELETE.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_REQUEST_FETCH</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> request to be of type FETCH.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_REQUEST_PATCH</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> request to be of type PATCH.
</p>
</td></tr><tr><td style="" valign="top">
<p>
<span class="strong"><strong>COAP_REQUEST_IPATCH</strong></span>
</p>
</td><td style="" valign="top">
<p>
Set the <span class="emphasis"><em>PDU</em></span> request to be of type IPATCH.
</p>
</td></tr></tbody></table></div><p>The <span class="emphasis"><em>message_id</em></span> must be unique per request (which is not the same as the
token), and must not be reused within EXCHANGE_LIFETIME (usually 247 seconds).
To automate this, the function coap_new_message_id(session) should be called.</p><p>The <span class="emphasis"><em>max_size</em></span> parameter defines the maximum size of a <span class="emphasis"><em>PDU</em></span> and is usually
determined by calling coap_session_max_pdu_size(session);</p><p>The <span class="strong"><strong>coap_add_token</strong></span>() function adds in the specified token’s <span class="emphasis"><em>data</em></span> of length
<span class="emphasis"><em>length</em></span> to the PDU <span class="emphasis"><em>pdu</em></span>.  The maximum length of the token is 8 bytes.
Adding the token must be done before any options or data are added.
This function must only be called once per <span class="emphasis"><em>pdu</em></span>, and must not be called
in the appropriate response handler.</p><p>The <span class="strong"><strong>coap_new_optlist</strong></span>() function returns a newly created <span class="emphasis"><em>optlist</em></span> entry of
type <span class="emphasis"><em>coap_optlist_t</em></span>*.  The <span class="emphasis"><em>number</em></span> specifies which CoAP option is to be
used, and is one of the COAP_OPTION_* definitions.  The <span class="emphasis"><em>length</em></span> is the length
of the data of the option, and <span class="emphasis"><em>data</em></span> points to the content of the option.</p><p>The following is the current list of options with their numeric value</p><pre class="screen">#define COAP_OPTION_IF_MATCH        1 /* opaque, 0-8 B */
#define COAP_OPTION_URI_HOST        3 /* String, 1-255 B */
#define COAP_OPTION_ETAG            4 /* opaque, 1-8 B */
#define COAP_OPTION_IF_NONE_MATCH   5 /* empty,  0 B */
#define COAP_OPTION_OBSERVE         6 /* empty/uint, 0 B/0-3 B */
#define COAP_OPTION_URI_PORT        7 /* uint, 0-2 B */
#define COAP_OPTION_LOCATION_PATH   8 /* String, 0-255 B */
#define COAP_OPTION_URI_PATH       11 /* String, 0-255 B */
#define COAP_OPTION_CONTENT_FORMAT 12 /* uint, 0-2 B */
#define COAP_OPTION_MAXAGE         14 /* uint, 0-4 B, default 60 Seconds */
#define COAP_OPTION_URI_QUERY      15 /* String, 1-255 B */
#define COAP_OPTION_ACCEPT         17 /* uint, 0-2 B */
#define COAP_OPTION_LOCATION_QUERY 20 /* String, 0-255 B */
#define COAP_OPTION_BLOCK2         23 /* uint, 0-3 B */
#define COAP_OPTION_BLOCK1         27 /* uint, 0-3 B */
#define COAP_OPTION_SIZE2          28 /* uint, 0-4 B */
#define COAP_OPTION_PROXY_URI      35 /* String, 1-1034 B */
#define COAP_OPTION_PROXY_SCHEME   39 /* String, 1-255 B */
#define COAP_OPTION_SIZE1          60 /* uint, 0-4 B */
#define COAP_OPTION_NORESPONSE    258 /* uint, 0-1 B */</pre><p>See FURTHER INFORMATION as to how to get the latest list.</p><p>The <span class="strong"><strong>coap_insert_optlist</strong></span>() function adds the <span class="emphasis"><em>optlist</em></span> entry onto the
<span class="emphasis"><em>optlist_chain</em></span> and then sorts the <span class="emphasis"><em>optlist_chain</em></span> before returning.
The initial <span class="emphasis"><em>optlist_chain</em></span> entry needs to be set to NULL before this function
is first called.  The coap_delete_optlist() function has to be called to free
off all the <span class="emphasis"><em>optlist_chain</em></span> entries.</p><p>The <span class="strong"><strong>coap_delete_optlist</strong></span>() function deletes and frees off all the optlist
entries in the <span class="emphasis"><em>optlist_chain</em></span>.</p><p>The <span class="strong"><strong>coap_add_optlist_pdu</strong></span>() function sorts all of the entries in
<span class="emphasis"><em>optlist_chain</em></span> into ascending option numeric order and adds all the entries
to the <span class="emphasis"><em>pdu</em></span>.  This function does not free off the entries in <span class="emphasis"><em>optlist_chain</em></span>.
This function must be called after adding any token and before adding in the
payload data.</p><p>The <span class="strong"><strong>coap_add_option</strong></span>() function adds in the specified option of type <span class="emphasis"><em>number</em></span>
with <span class="emphasis"><em>data</em></span> of length <span class="emphasis"><em>length</em></span> to the PDU <span class="emphasis"><em>pdu</em></span>.
It is critical that options are added to the <span class="emphasis"><em>pdu</em></span> with <span class="emphasis"><em>number</em></span> either
being the same or greater than the previous option <span class="emphasis"><em>number</em></span> that was added.</p><p>The <span class="strong"><strong>coap_add_data</strong></span>() function adds in the specified payload <span class="emphasis"><em>data</em></span> of length
<span class="emphasis"><em>length</em></span> to the PDU <span class="emphasis"><em>pdu</em></span>. Adding the payload data must be done after any
token or options are added.  This function must only be called once per <span class="emphasis"><em>pdu</em></span>.</p><p>The <span class="strong"><strong>coap_add_data_blocked_response</strong></span>() function adds in the appropriate part
of the payload <span class="emphasis"><em>data</em></span> of length <span class="emphasis"><em>length</em></span> to the PDU <span class="emphasis"><em>pdu</em></span>. It should be used
as a direct replacement for <span class="strong"><strong>coap_add_data</strong></span>() if it is possible that the data
will not fit into a single pdu. It also adds in the appropriate
CoAP options to handle Block-Wise transfer. This function is usually used for
a server’s GET response.  The <span class="emphasis"><em>resource</em></span>, <span class="emphasis"><em>session</em></span>, <span class="emphasis"><em>request</em></span>, <span class="emphasis"><em>response</em></span>
and <span class="emphasis"><em>token</em></span> are the same parameters for the registered GET resource handler.
The <span class="emphasis"><em>media_type</em></span> is for the format of the <span class="emphasis"><em>data</em></span> and <span class="emphasis"><em>maxage</em></span> defines the
lifetime of the response.  If set to -1,  then the MAXAGE option does not get
included.  This function must only be called once per <span class="emphasis"><em>pdu</em></span>.
It is the responsibility of the client to recognize that it has only
received a part of the data and request the next block (with the appropriate
Block options) from the server.  Returning the next requested block is handled
by this function.</p><p>The <span class="strong"><strong>coap_encode_var_safe</strong></span>() function encodes <span class="emphasis"><em>value</em></span> into <span class="emphasis"><em>buffer</em></span> which has
a size of <span class="emphasis"><em>size</em></span> in bytes.  Normally, the <span class="emphasis"><em>buffer</em></span> size should be at least
the sizeof(int) bytes unless you definitely know less space is required.</p><p>The <span class="strong"><strong>coap_split_path</strong></span>() function splits up <span class="emphasis"><em>path</em></span> of length <span class="emphasis"><em>length</em></span> and
places the result in <span class="emphasis"><em>buffer</em></span> which has a size of <span class="emphasis"><em>buflen</em></span>.  <span class="emphasis"><em>buflen</em></span> needs
to be preset with the size of <span class="emphasis"><em>buffer</em></span> before the function call, and then
<span class="emphasis"><em>buflen</em></span> is updated with the actual size of <span class="emphasis"><em>buffer</em></span> used.</p><p>The <span class="strong"><strong>coap_split_query</strong></span>() function splits up <span class="emphasis"><em>query</em></span> of length <span class="emphasis"><em>length</em></span> and
places the result in <span class="emphasis"><em>buffer</em></span> which has a size of <span class="emphasis"><em>buflen</em></span>.  <span class="emphasis"><em>buflen</em></span> needs
to be preset with the size of <span class="emphasis"><em>buffer</em></span> before the function call, and then
<span class="emphasis"><em>buflen</em></span> is updated with the actual size of <span class="emphasis"><em>buffer</em></span> used.</p></div><div class="refsect1"><a id="_return_values"></a><h2>RETURN VALUES</h2><p>The <span class="strong"><strong>coap_pdu_init</strong></span>() function returns a newly created <span class="emphasis"><em>PDU</em></span> or NULL if there
is a malloc or parameter failure.</p><p>The <span class="strong"><strong>coap_new_optlist</strong></span>() function returns a newly created <span class="emphasis"><em>optlist</em></span> or NULL
if there is a malloc failure.</p><p>The <span class="strong"><strong>coap_add_token</strong></span>(), <span class="strong"><strong>coap_insert_optlist</strong></span>(), <span class="strong"><strong>coap_delete_optlist</strong></span>(),
<span class="strong"><strong>coap_add_optlist_pdu</strong></span>() and <span class="strong"><strong>coap_add_data</strong></span>()
functions return 0 on failure, 1 on success.</p><p>The <span class="strong"><strong>coap_add_optlist</strong></span>() function returns either the length of the option
or 0 on failure.</p><p>The <span class="strong"><strong>coap_encode_var_safe</strong></span>() function returns either the length of bytes
encoded or 0 on failure.</p></div><div class="refsect1"><a id="_examples"></a><h2>EXAMPLES</h2><p><span class="strong"><strong>Setup PDU and Transmit</strong></span></p><pre class="programlisting">#include &lt;coap2/coap.h&gt;

static int token = 0;

int
build_send_pdu(coap_context_t *context, coap_session_t *session,
uint8_t msgtype, uint8_t request_code, const char *uri, const char *query,
unsigned char *data, size_t length, int observe) {

  coap_pdu_t *pdu;
  (void)context;
  char buf[1024];
  size_t buflen;
  char *sbuf = buf;
  int res;
  coap_optlist_t *optlist_chain = NULL;

  /* Create the pdu with the appropriate options */
  pdu = coap_pdu_init(msgtype, request_code, coap_new_message_id(session),
                      coap_session_max_pdu_size(session));
  if (!pdu)
    return 0;

  /*
   * Create uniqueness token for this request for handling unsolicited /
   * delayed responses
   */
  token++;
  if (!coap_add_token(pdu, sizeof(token), (unsigned char*)&amp;token)) {
    coap_log(LOG_DEBUG, "cannot add token to request\n");
    goto error;
  }

  if (uri) {
    /* Add in the URI options */
    buflen = sizeof(buf);
    res = coap_split_path((const uint8_t*)uri, strlen(uri), sbuf, &amp;buflen);
    while (res--) {
      if (!coap_insert_optlist(&amp;optlist_chain,
                               coap_new_optlist(COAP_OPTION_URI_PATH,
                        coap_opt_length(sbuf), coap_opt_value(sbuf))))
        goto error;
      sbuf += coap_opt_size(sbuf);
    }
  }

  if (query) {
    /* Add in the QUERY options */
    buflen = sizeof(buf);
    res = coap_split_query((const uint8_t*)query, strlen(query), sbuf, &amp;buflen);
    while (res--) {
      if (!coap_insert_optlist(&amp;optlist_chain,
                               coap_new_optlist(COAP_OPTION_URI_QUERY,
                        coap_opt_length(sbuf), coap_opt_value(sbuf))))
        goto error;
      sbuf += coap_opt_size(sbuf);
    }
  }

  if (request_code == COAP_REQUEST_GET &amp;&amp; observe) {
    /* Indicate that we want to observe this resource */
    if (!coap_insert_optlist(&amp;optlist_chain,
                             coap_new_optlist(COAP_OPTION_OBSERVE,
                                         COAP_OBSERVE_ESTABLISH, NULL)))
      goto error;
  }

  /* ... Other code / options etc. ... */

  /* Add in all the options (after internal sorting) to the pdu */
  if (!coap_add_optlist_pdu(pdu, &amp;optlist_chain))
    goto error;

  if (data &amp;&amp; length) {
    /* Add in the specified data */
    if (!coap_add_data(pdu, length, data))
      goto error;
  }

  if (coap_send(session, pdu) == COAP_INVALID_TID)
    goto error;
  return 1;

error:

  if (pdu)
    coap_pdu_delete(pdu);
  return 0;

}</pre><p><span class="strong"><strong>Resource Handler Response PDU Update</strong></span></p><pre class="programlisting">#include &lt;coap2/coap.h&gt;

static void
hnd_get_time(coap_context_t *context, coap_resource_t *resource,
coap_session_t *session, coap_pdu_t *request, coap_string_t *token,
coap_string_t *query, coap_pdu_t *response) {

  unsigned char buf[40];
  size_t len;
  time_t now;

  /* ... Additional analysis code for resource, request pdu etc.  ... */

  /* After analysis, generate a suitable response */

  /* Note that token, if set, is already in the response pdu */

  now = time(NULL);

  if (query != NULL &amp;&amp; coap_string_equal(query, coap_make_str_const("secs"))) {
    /* Output secs since Jan 1 1970 */
    len = snprintf((char *)buf, sizeof(buf), "%lu", now);
  }
  else {
    /* Output human-readable time */
    struct tm *tmp;
    tmp = gmtime(&amp;now);
    if (!tmp) {
      /* If 'now' is not valid */
      response-&gt;code = COAP_RESPONSE_CODE(404);
      return;
    }
    len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
  }
  /*
   * Invoke coap_add_data_blocked_response() to do all the hard work.
   *
   * Define the format - COAP_MEDIATYPE_TEXT_PLAIN - to add in
   * Define how long this response is valid for (secs) - 1 - to add in.
   *
   * OBSERVE Option added internally if needed within the function
   * BLOCK2 Option added internally if output too large
   * ETAG Option added internally
   */
  coap_add_data_blocked_response(resource, session, request, response, token,
                                 COAP_MEDIATYPE_TEXT_PLAIN, 1,
                                 len,
                                 buf);

  /*
   * As resource-&gt;code has been updated in coap_add_data_blocked_response(),
   * the response pdu will be transmitted by the underlying library.
   */

}</pre></div><div class="refsect1"><a id="_see_also"></a><h2>SEE ALSO</h2><p><a href="man_coap_observe.html" target="_self"><span class="strong"><strong>coap_observe</strong></span>(3)</a>, <a href="man_coap_resource.html" target="_self"><span class="strong"><strong>coap_resource</strong></span>(3)</a></p></div><div class="refsect1"><a id="_further_information"></a><h2>FURTHER INFORMATION</h2><p>See "RFC7252: The Constrained Application Protocol (CoAP)" for further
information.</p><p>See <a class="ulink" href="https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#option-numbers" target="_top">https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#option-numbers</a>
for the current set of defined CoAP Options.</p></div><div class="refsect1"><a id="_bugs"></a><h2>BUGS</h2><p>Please report bugs on the mailing list for libcoap:
<a class="ulink" href="mailto:libcoap-developers@lists.sourceforge.net" target="_top">libcoap-developers@lists.sourceforge.net</a></p></div><div class="refsect1"><a id="_authors"></a><h2>AUTHORS</h2><p>The libcoap project &lt;<a class="ulink" href="mailto:libcoap-developers@lists.sourceforge.net" target="_top">libcoap-developers@lists.sourceforge.net</a>&gt;</p></div></div></body></html> coap_pdu_setup(3)</p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Fri Dec 27 2024 09:57:44 for libcoap by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.8 </li>
  </ul>
</div>
</body>
</html>
